package com.guinea.skoru.service;
// Generated 2011-3-19 15:17:31 by Hibernate Tools 3.4.0.Beta1

import java.io.Serializable;
import java.util.List;

import com.guinea.core.orm.hibernate.HibernateDao;
import com.guinea.core.service.CRUDManager;
import com.guinea.skoru.dao.SkoruRegionDao;
import com.guinea.skoru.entity.SkoruRegion;
import com.guinea.skoru.entity.SkoruTown;

public class SkoruRegionManager extends CRUDManager {

	private SkoruRegionDao skoruRegionDao;

	public HibernateDao getEntityDao() {
		return getSkoruRegionDao();
	}

	public SkoruRegion getSkoruRegion(Serializable id) {
		return (SkoruRegion)getSkoruRegionDao().get(id);
	}

	public SkoruRegionDao getSkoruRegionDao() {
		return skoruRegionDao;
	}

	public void setSkoruRegionDao(SkoruRegionDao skoruRegionDao) {
		this.skoruRegionDao = skoruRegionDao;
	}

	public SkoruTown getMatchedTown(String provinceId,String cityId,String districtId){
		SkoruTown town = null;
		if(provinceId == null || cityId == null) return town;
		String hql1 = "select r from SkoruTown r where r.provinceId = ? and r.cityId = ? and r.districtId = ? and r.status = '1'";
		String hql2 = "select r from SkoruTown r where r.provinceId = ? and r.cityId = ? and r.districtId is null  and r.status = '1'";
		List l = null;
		//有区的先查到区级
		if(districtId != null){
			l = getSkoruRegionDao().find(hql1,new Object[]{provinceId,cityId,districtId});
		}
		//区级查不到再查到市级
		if(l == null || l.size() < 1){
			l = getSkoruRegionDao().find(hql2,new Object[]{provinceId,cityId});
		}
		if(l != null && l.size() > 0){
			town = (SkoruTown)l.get(0);
		}
		return town;
	}
}
